<?php 
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;

$user = $response->getAttribute("user");
$order = $response->getAttribute('invoiceOrder');
$currency = $order->getCurrency();
$currencySymbol = $currency->getSymbol();

$recalculate=false;
$title = $languageManager->getText('INVOICE');
if (!$order->ispaid()) {
	$title = $languageManager->getText('ORDER');
	$recalculate=true;
}

$orderArray = ShopAction::getOrder($order, $recalculate);
$customerEmail = $user->getEmail();

$customerName = $orderArray['customerName'];
$customerAddress = $orderArray['customerAddress'];
$customerZipCode = $orderArray['customerZipCode'];
$customerCity = $orderArray['customerCity'];

$password = $response->getAttribute('paymentPassword');
$paymentUrl = $response->getAttribute('paymentUrl');
?>
<article id="cont-princ" class="nocols">
	<?php $templateManager->displayTemplate('Steps');  ?>
	<h1><?php Printer::display($languageManager->getText('PAYMENT')); ?></h1>
	<div id="facturation" class="paiement">
		<table class="factAdresse">
			<thead>
			    <tr>
				    <th colspan="2"><?php Printer::display($languageManager->getText('INVOICE_ADDRESS')); ?></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><b><?php Printer::display($customerName); ?></b></td>
					<td rowspan="3"><a href="<?php echo HTML::linkHref('action=EditUser'); ?>"><?php Printer::display($languageManager->getText('MODIFY')); ?></a></td>
				</tr>
				<tr>
					<td><?php Printer::display($customerAddress); ?></td>
				</tr>
				<tr>
					<td><?php Printer::display($customerZipCode.' '.$customerCity); ?></td>
				</tr>
			</tbody>
		</table>
		<br />
		<table class="factTotal">
			<tbody>
			    <tr>
					<td><b><?php Printer::display($languageManager->getText('PRICE_WO_TAXES')); ?></b></td>
					<td class="prix">
					    <?php Printer::display(number_format($orderArray['realTotal'], 2, '.', ' ').$currencySymbol); ?>
				    </td>
				</tr>
				<?php 
				foreach ($orderArray['taxes'] as $tax) {
				?>
				<tr>
					<td>
					    <b>
					        <?php Printer::display($tax['tax']->getName().' ('.number_format($tax['value'], 2, '.', '').'%)'); ?>
				        </b>
				    </td>
					<td class="prix">
					    <?php Printer::display(number_format($tax['total'], 2, '.', ' ').$currencySymbol); ?>
				    </td>
				</tr>
				<?php 
				}
				?>
				<tr>
					<td class="total"><b><?php Printer::display($languageManager->getText('PRICE_W_TAXES')); ?></b></td>
					<td class="prix total">
					    <?php 
					        Printer::display(number_format($orderArray['paymentTotal'], 2, '.', ' ').$currencySymbol);
				        ?>
				    </td>
				</tr>
			</tbody>
		</table>
		<table class="factMentions">
			<tbody>
				<tr>
					<td>
						<b>Groupe Allo-Media - 3 villa du Poitou - 95150 Taverny</b><br />
						SIRET : 53116321000010
					</td>
				</tr>
			</tbody>
		</table>
		
	</div>
	
	<?php 
	if (is_null($order->getStatus())) {
	?>
	<div id="factReglement">
	    <h3><?php Printer::display($languageManager->getText('PAYMENT_MEAN')); ?></h3>
	    <?php
	    if (!isNullOrEmptyString($customerName)
            && !isNullOrEmptyString($customerEmail)
            && !isNullOrEmptyString($customerAddress)
            && !isNullOrEmptyString($customerCity)
            && !isNullOrEmptyString($customerZipCode)
        ) {
	    ?>
		<div class="col col1">
			<div class="col-cont">
			    <h4><?php Printer::display($languageManager->getText('CREDIT_CARD_PAYMENT')); ?> :</h4>
			    <form action="<?php echo $paymentUrl; ?>" method="POST">
		        <?php
		        $paymentParams = array();
		        $paymentParams['ACCEPTURL'] = Request::getInstance()->getHostURI().'?action=ShowInvoice&order_id='.$order->getId();
		        $paymentParams['AMOUNT'] = number_format($orderArray['paymentTotal']*100, 0, '.', '');
		        $paymentParams['BACKURL'] = Request::getInstance()->getHostURI().'?action=CheckoutBasket';
		        //$paymentParams['BGCOLOR'] = '';
		        //$paymentParams['BUTTONBGCOLOR'] = '';
		        //$paymentParams['BUTTONTXTCOLOR'] = '';
		        $paymentParams['CANCELURL'] = Request::getInstance()->getHostURI();
		        $paymentParams['CN'] = $customerName;
		        $paymentParams['CURRENCY'] = $currency->getIso();
		        $paymentParams['DECLINEURL'] = Request::getInstance()->getHostURI();
		        $paymentParams['EMAIL'] = $customerEmail;
		        $paymentParams['EXCEPTIONURL'] = Request::getInstance()->getHostURI();
		        //$paymentParams['FONTTYPE'] = '';
		        $paymentParams['LANGUAGE'] = 'fr_FR';
		        //$paymentParams['LOGO'] = '';
		        $paymentParams['ORDERID'] = $orderArray['number'];
		        $paymentParams['OWNERADDRESS'] = $customerAddress;
		        $paymentParams['OWNERTOWN'] = $customerCity;
		        $paymentParams['OWNERZIP'] = $customerZipCode;
		        $paymentParams['PSPID'] = 'allomedia';
		        //$paymentParams['TBLBGCOLOR'] = '';
		        //$paymentParams['TBLTXTCOLOR'] = '';
		        //$paymentParams['TITLE'] = '';
		        //$paymentParams['TXTCOLOR'] = '';
		        
		        $shaString = '';
		        foreach ($paymentParams as $paramName=>$paramValue) {
		            $shaString .= $paramName.'='.$paramValue.$password;
		        ?>
		            <input type="hidden" name="<?php Printer::display($paramName); ?>" value="<?php Printer::display($paramValue); ?>" />
		        <?php
		        }
		        
		        $sha1Code = sha1($shaString);
		        ?>
			        <input type="hidden" name="SHASign" value="<?php Printer::display($sha1Code); ?>">
			        <input type="submit" class="visa_card" value="" />
			        <input type="submit" class="cb_card" value="" />
			        <input type="submit" class="mastercard" value="" />
			    </form>
			</div>
		</div>
		<div class="col col2">
			<div class="col-cont">
				<p class="hsbc" ></p>
			</div>
		</div>
		<!-- 
		<div class="col col2">
			<div class="col-cont">
				<h4><?php Printer::display($languageManager->getText('OTHER_MEAN_OF_PAYMENT')); ?></h4>
				<p>
					<a href="#" class="btn boxshadow"><?php Printer::display($languageManager->getText('PAYMENT_BY_CHEQUE')); ?></a>
					<a href="#" class="btn boxshadow"><?php Printer::display($languageManager->getText('PAYMENT_BY_TRANSFER')); ?></a>
				</p>
			</div>
		</div>
		-->
		<?php 
		} else {
			echo HTML::link(
					$languageManager->getText('FILL_PERSONAL_INFO_TO_FINALIZE'),
					'action=EditUser'
			);
		}
		?>
	</div>
	<?php 
	}
	?>
</article>
<script type="text/javascript">
	$(document).ready(function(){
			
		// #panierEtape First-Last Child
		$('#panierEtape .etape:first-child .cont').addClass('prem');
		$('#panierEtape .etape:last-child .cont').addClass('dern');
		
	});
</script>
